using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class manager_template_view : System.Web.UI.Page
{
    private string id = "";
    string user_present = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        // test user login yet
        user_present = (string)Session["Admin"];
        if (user_present == null)
        {
            Response.Redirect("../login/login.aspx");
        }

        id = Request["id"];
        if (id == "new")
        {

        }
        else
        {
            if (Page.IsPostBack == false)
            {
                BindData();
                load_site();
            }
            else return;
        }

    }

    protected void BindData()
    {
        Template tem = Template.get_template(Convert.ToInt32(id));
        txt_Name.Text = tem.Name;
        txt_Path.Text = tem.Path;
        ddl_site.Items.FindByValue(tem.ID.ToString()).Selected=true;
    }

    void load_site()
    {
        ddl_site.DataSource = SiteDomain.get_all_site();
        ddl_site.DataTextField = "Domain";
        ddl_site.DataValueField = "ID";
        ddl_site.DataBind();
    }

    protected void execute(object sender, EventArgs e)
    {
        Template tem;
        int adminID = Admin.get_admin(user_present).ID;
        if (id == "new")
        {
            try
            {
                tem = new Template();
                tem.SiteID = Convert.ToInt32(ddl_site.SelectedValue);
                tem.Name = txt_Name.Text;
                tem.Path = txt_Path.Text;
                tem.CreatedUserID = adminID;
                tem.UpdatedUserID = adminID;
                tem.Create();
                //ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('Create new site successful.');</script>");
                Functions.message_box(this, "Create successful.");
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");
            }
        }
        else
        {
            try
            {
                tem = Template.get_template(Convert.ToInt32(id));
                tem.SiteID = Convert.ToInt32(ddl_site.SelectedValue);
                tem.Name = txt_Name.Text;
                tem.Path = txt_Path.Text;
                tem.CreatedUserID = adminID;
                tem.UpdatedUserID = adminID;
                tem.UpdatedDate = DateTime.Now;
                tem.Update();
                Functions.message_box(this, "Update successful.");
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");
            }
        }
    }
}
